main.controller('attrCtrl', ['$scope', function ($scope) {
	console.log('attr.js')
}]);
main.controller('dashBroadCtrl', ['$scope','$rootScope','$timeout','NoticeServ','tools', function($scope,$rootScope,$timeout,NoticeServ,tools) {
	console.log('dashBroad.js');

	$timeout(function(){
		NoticeServ.$broadcast('GLOBAL_CATEGORY_CHANGE_EV', "/");
	},1000);

	
	
	NoticeServ.$on($scope,'LOGIN_EV',function(){
		$rootScope.$broadcast('GLOBAL_CATEGORY_CHANGE_EV', "/");
	});
	
	$scope.cardsconfig = {
		cardTitle:[''],
		icin:''

	}
	$scope.configline = {
		title: '全网类目销售额走势图',
		option: {
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow'
				},
				formatter: function(params) {
					
					var str = '<div style = "text-align: left;">' + params[0].name + ' 销售额</div>';

					for (var i = 0; i < params.length; i++) {
						str += '<div style = "text-align: left;padding-left:10px;"><span style="width:10px;height:10px;display:inline-block;border-radius:50%;background:'+params[i].color+'"></span>  ' + params[i].seriesName + ' : ' + tools.bigNumFormat(params[i].data) + '</div>';
					}

					return str;
				}
			},
			grid: {
				left: 80
			},
			legend: {
				data: ['总和', '天猫', '淘宝','天猫国际','全球购'],
				x: 25,
				y: 15
			},
			xAxis: {
				data: [],
				axisLabel: {
                    //rotate: 50,
                    show: true
                }
            },
            yAxis: [{
            	type: 'value',
            	axisLabel: {
            		formatter: function(value, index) {
            			var len = String(value).length;

            			if (value < 10000) {
            				return value;
            			}

            			switch (len) {
            				case 5:
            				return (value / 10000) + '万';
            				case 6:
            				return (value / 10000) + '万';
            				case 7:
            				return (value / 10000) + '万';
            				case 8:
            				return (value / 10000) + '万';
            				case 9:
            				return (value / 100000000) + '亿';
            				case 10:
            				return (value / 100000000) + '亿';
            				case 11:
            				return (value / 100000000) + '亿';
            				case 12:
            				return (value / 100000000) + '亿';
            			}

            			return value;
            		}
            	}
            }],
            series: [
	            {
	            	name: '总和',
	            	type: 'line',
	            	data: []
	            },
	            {
	            	name: '天猫',
	            	type: 'line',
	            	data: []
	            },	            
	            {
	            	name: '淘宝',
	            	type: 'line',
	            	data: [],
	            	label: {
	            		normal: {
	            			formatter: function(value) {
	            				return '元';
	            			}
	            		},
	            		emphasis: function(value) {
	            			return '33';
	            		}
	            	}
	            },
	            {
	            	name: '天猫国际',
	            	type: 'line',
	            	data: []
	            }, {
	            	name: '全球购',
	            	type: 'line',
	            	data: []
	            }
            ]
        },
        get:function(scope,myChart,catName){
        	myChart.clear();
        	myChart.showLoading();
        	this.option.xAxis.data = [];
        	this.option.series[0].data = [];
        	this.option.series[1].data = [];
        	this.option.series[2].data = [];
        	this.option.series[3].data = [];
        	this.option.series[4].data = []
        	var $option = this.option;
        	tools.$get({
                url: '/marketconditions/statistic/getCategoryStatisticIndex.htm',
                data: {
                    catName: catName
                }
            }).then(function(resp){
            	console.log('78')
            	console.log(resp);
            	var dim = "sellMoney";
            	
                for (var i = 0; i < resp.value.length; i++) {
                    $option.xAxis.data.push(resp.value[i].date.split(' ')[0].slice(5, 20));
                    $option.series[0].data.push(resp.value[i][dim]);
                    $option.series[1].data.push(resp.value[i]['t' + dim[0].toUpperCase() + dim.slice(1)]);
                    $option.series[2].data.push(resp.value[i]['c' + dim[0].toUpperCase() + dim.slice(1)]);
                    $option.series[3].data.push(resp.value[i]['thk' + dim[0].toUpperCase() + dim.slice(1)]);
                    $option.series[4].data.push(resp.value[i]['g' + dim[0].toUpperCase() + dim.slice(1)]);
                }
                myChart.setOption($option);
                myChart.hideLoading();   

            },function(errResp){

            });
                
        },
        handel: function() {

        }
    };
}]);

main.controller('lastyCtrl', ['$scope', function ($scope) {
	console.log('lasty');
}]);
main.controller('loginCtrl', ['$scope','tools','$cookies','$timeout','Session','$location','SweetAlert','NoticeServ','$http','$rootScope', function ($scope, tools,$cookies,$timeout,Session,$location,SweetAlert,NoticeServ,$http,$rootScope) {

	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}

	$rootScope.userInfo = {};

	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		remember:false,
		rememberStuff:false,
		nameError:'',
		passwordError:'',
		init:function(){
			var data = $cookies.get('user');
			console.log(data);
			if(data){
				data=JSON.parse(data);
				this.name = data.name;
				this.password = data.password;
				this.remember = data.remember; 
				this.rememberStuff = true;
			}
		},
		isRemember:function(){			
			
			console.log(this.remember);
			if(this.remember){
				var data = {
					name:this.name,
					password:this.password,
					remember:true
				};

				$cookies.put('user',JSON.stringify(data));
				console.log($cookies.get('user'));
			}else{
				console.log('remove cookie');
				$cookies.remove('user');
			}
			
		},
		checkName:function(){
			
		},
		login:function(){

			if(this.nameError !='' || this.name == '' || this.password == ''){
				return;
			}
			var $this = this;
			console.log(this.remember);
			tools.$get({
				url:'/marketconditions/user/userLogin.htm',
				data:{
					phone:this.name,
					password:this.password
				}
			}).then(function(resp){
				console.log('++++')
				console.log(resp);
				$this.passwordError = "";
				$this.isRemember();
				Session.create(resp.value);
				$rootScope.userInfo = resp.value
				$location.path('/dashBroad');
				NoticeServ.$broadcast('LOGIN_EV');
				// SweetAlert.swal({
				// 	title: "登录成功",
				// 	type: "success",
				// 	showCancelButton: false,
				// 	confirmButtonColor: "#DD6B55",
				// 	confirmButtonText: "去首页",
				// 	closeOnConfirm: true
				// },function(){ 
				// 	$timeout(function(){
				// 		$location.path('/share');
				// 		$rootScope.$broadcast('LOGIN_SUCCESS_EV');
				// 	},100);
				// });
			
			},function(errResp){
				console.log(errResp);


				if(errResp.message == "alreadyEnter"){
					SweetAlert.swal('','该用户已登录','error');
				}else{
					$this.passwordError = "密码错误";
					SweetAlert.swal('','账号或密码错误','error');
				}

				
				// console.log(errResp)
				console.log('error');
			});
		}
	};

	$scope.User.init();
}]);

main.controller('regCtrl', ['$scope','tools','SweetAlert','$location','$timeout', function ($scope, tools,SweetAlert,$location,$timeout) {


	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$('ul.tabs').tabs();
	$timeout(function(){
		$('ul.tabs').tabs('select_tab','phoneReg');
	},100);
	


	$scope.scond = 60;
	function setTime(){
        if($scope.scond!=0){
            $scope.scond--;
            $timeout(function(){                
                setTime();
            },1000);
        }else{
            $scope.User.getCodeStuff = false;
            $scope.scond = 60;
        }
    }
	// SweetAlert.swal('','注册失败','error');
	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		password2:'',
		phone:'',
		email:'',
		nameError:'',
		passwordError:'',
		phoneError:'',
		emailError:'',
		getCodeStuff:false,
		getCode:function(){
			this.getCodeStuff = true;
			$timeout(function(){
				setTime();
			},1000);
		},
		checkName:function(){
			console.log(this.name);
			console.log(this.oldName);
			if(this.name == '' || this.name == this.oldName){
				return;
			}
			
			var $this = this;
			tools.$post({
				url:'/users/queryByName',
				data:{
					name:this.name
				}
			}).then(function(resp){
				$this.oldName = $this.name;
				console.log(resp);
				if(resp.value.length == 0){
					$this.nameError = "";
				}else{
					$this.nameError = "该用户名已存在";
				}
				
			},function(errResp){

				console.log('error')
			});
		},
		checkPassword:function(){
			if(this.password != this.password2){
				this.passwordError = '两次密码不一致';
				
			}else{
				this.passwordError = '';

			}
		},
		checkEmail:function(){
			var regExp = new RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/);
			if(regExp.test(this.email)){
				this.emailError = '';
			}else{
				this.emailError = '邮箱格式错误';
			}
		},
		checkPhone:function(){
			var regExp = new RegExp(/^1[34578]\d{9}$/);
			if(regExp.test(this.phone)){
				this.phoneError = '';
			}else{
				this.phoneError = '手机格式错误';
			}
		},
		reg:function(){

			this.checkPassword();
			if(this.nameError !='' || this.passwordError != '' || this.emailError !='' || this.name=='' || this.password=='' || this.email ==''){
				SweetAlert.swal('','请填写正确的注册信息','warning');
				return;
			}
			tools.$post({
				url:'/users/insert',
				// url:'/proxy/marketconditions/user/userLogin.htm',
				data:{
					name: this.name,
					password: this.password,
					email: this.email
				}
			}).then(function(resp){
				console.log(resp);
				if(resp.value.insertId){
					SweetAlert.swal({
						title: "注册成功",
						type: "success",
						showCancelButton: false,
						confirmButtonColor: "#DD6B55",
						confirmButtonText: "去登录",
						closeOnConfirm: true
					},function(){ 
						$timeout(function(){
							$location.path('/login');
						},100);
					});
				}else{
					SweetAlert.swal('','注册失败','error');
				}
				
			},function(errResp){
				console.log('error');
			});
		}
	};


}]);